part of dartendo;

class Mapper068 extends MapperDefault {
  int r1 = 0;
  int r2 = 0;
  int r3 = 0; 
  int r4 = 0;

  Mapper068(NES nes_) : super(nes_);

  void write(int address, int value) {
    if (address < 0x8000) {
      super.write(address, value);
      return;
    }

    switch ((address >> 12) - 0x8) {

      case 0:
                // Select 2K VROM bank at 0x0000
                load2kVromBank(value, 0x0000);
                break;

      case 1:
                // Select 2K VROM bank at 0x0800
                load2kVromBank(value, 0x0800);
                break;

      case 2:
                // Select 2K VROM bank at 0x1000
                load2kVromBank(value, 0x1000);
                break;

      case 3:
                // Select 2K VROM bank at 0x1800
                load2kVromBank(value, 0x1800);
                break;

      case 4:
                // Mirroring.
                r3 = value;
                setMirroring();
                break;

      case 5:
                // Mirroring.
                r4 = value;
                setMirroring();
                break;

      case 6:
                // Mirroring.
                r1 = (value >> 4) & 0x1;
                r2 = value & 0x3;
                setMirroring();
                break;

      case 7:
                // Select 16K ROM bank at 0x8000
                loadRomBank(value, 0x8000);
                break;
    }
  }

  void setMirroring() {

    if (r1 == 0) {

      // Normal mirroring modes:
      switch (r2) {
        case 0:
                  ppu.setMirroring(ROM.HORIZONTAL_MIRRORING);
                  break;
        case 1:
                  ppu.setMirroring(ROM.VERTICAL_MIRRORING);
                  break;
        case 2:
                  ppu.setMirroring(ROM.SINGLESCREEN_MIRRORING);
                  break;
        case 3:
                  ppu.setMirroring(ROM.SINGLESCREEN_MIRRORING2);
                  break;
      }

    } else {

      // Special mirroring (not yet..):
      switch (r2) {
        case 0:
                  break;
        case 1:
                  break;
        case 2:
                  break;
        case 3:
                  break;
      }

    }

  }

  void loadROM(ROM rom_) {
    assert(rom_ == rom);

    //System.out.println("Loading ROM.");

    if (!rom.isValid()) {
      print("Mapper068.loadROM: Sunsoft#4: Invalid ROM! Unable to load.");
      return;
    }

    // Get number of PRG ROM banks:
    int num_banks = rom.getRomBankCount();

    // Load PRG-ROM:
    loadRomBank(0, 0x8000);
    loadRomBank(num_banks - 1, 0xC000);

    // Load CHR-ROM:
    loadCHRROM();

    // Load Battery RAM (if present):
    loadBatteryRam();

    // Do Reset-Interrupt:
    nes.getCpu().requestIrq(CPU.IRQ_RESET);
  }

  void reset() {
    r1 = r2 = r3 = r4 = 0;
  }
}
